package com.hdl.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.hdl.dao.UserDao;
import com.hdl.dao.impl.UserDaoImpl;
import com.hdl.dto.UserSearch;
import com.hdl.entity.HdlUser;
import com.hdl.service.UserService;
import com.hdl.vo.HdlUserVO;
import com.hdl.vo.PageVO;

public class UserServiceImpl implements UserService{
	
	private UserDao dao = new UserDaoImpl();
	
	@Override
	public HdlUser login(HdlUser loginUser) {
		return dao.login(loginUser);
	}

	@Override
	public void register(HdlUser user) {
		dao.register(user);
	}

	@Override
	public int active(String code) {
		return dao.active(code);
		
	}

	@Override

	public void update(HdlUser user) {
		dao.update(user);
	}

	@Override
	public HdlUser selectById(String userId) {
		
		return dao.selectById(userId);
	}
	
	public HdlUser selectUserById(String userId) {
		return dao.selectUserById(userId);

	}

	@Override

	public void UpdatePsd(String id, String psd) {
		dao.UpdatePsd(id, psd);
	}
		

	public HdlUser findPsdByEmail(String email) {
		
		return dao.findPsdByEmail(email);

	}

	@Override
	public boolean checkName(String userName) {
		
		return dao.checkName(userName);
	}

	@Override
	public int updates(HdlUser user) {
try {
	return dao.updateSelective(user);
} catch (Exception e) {
	e.printStackTrace();
	throw new RuntimeException("修改用户失败",e);
}
	}

	@Override
	public PageVO<HdlUserVO> page(UserSearch search) {
		try {
			PageVO<HdlUserVO> page=new PageVO<HdlUserVO>(dao.count(search), search.getPageNumber(),search.getPageSize());
		
			return page.setList(dao.select(search, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("分页失败",e);
		}
	}

	@Override
	public HdlUser get(String hdlUserId) {
		try {
			return dao.selectById(hdlUserId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("查询用户失败",e);
		}
	}


	@Override
	public int delete(String hdlUserId) {	
		try {
			return dao.delete(hdlUserId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("删除用户失败",e);
		}
	}

	@Override
	public List<HdlUser> list(int cureen, int roMax) {
		try {
			return dao.selectModel(null, (cureen-1)*roMax, roMax);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("获取分页失败",e);
		}
	}

	@Override
	public List<HdlUser> list(int len) {
		try {
			return  dao.selectModel(null, 0, len);
		} catch (Exception e) {
			throw new RuntimeException("获取用户列表失败",e);
		}
	}

}
